/**
 * Copyright (c) 2011-2012 Tomi Paananen.
 *
 * Part of Quick Gestures user interface.
 */

import QtQuick 1.1
import Qt.labs.folderlistmodel 1.0

Item {
    id: folderList

    property alias folder: folderModel.folder
    property bool hasParentFolder: (helper.urlToLocalFile(folderModel.folder).length > 0)
    property alias listView: folderListView // For attaching a scroll decorator

    signal folderSelected(variant folder)
    signal fileSelected(variant file)

    /**
     * Navigates to the parent folder of the current folder.
     */
    function toParentFolder()
    {
        if (hasParentFolder) {
            folderModel.folder = folderModel.parentFolder;
        }
    }

    // FolderListModel is not visible item.
    FolderListModel {
        id: folderModel
        folder: Qt.resolvedUrl("file:./"); // Use the current folder by default
        nameFilters: [ "*.xml" ] // Show only .xml files
    }

    Component {
        id: folderListHeader

        ListHeader {
            text: {
                var filePath = helper.urlToLocalFile(folderModel.folder);

                if (filePath.length) {
                    return filePath;
                }

                return "File system root";
            }

            elide: Text.ElideMiddle
        }
    }

    Component {
        id: folderListItem

        FolderListDelegate {
            isFolder: folderModel.isFolder(index);
            name: fileName

            onClicked: {
                if (!fileName.length) {
                    console.debug("FolderList.qml: Error: Empty filename!");
                    return;
                }

                if (folderModel.isFolder(index)) {
                    folderList.folderSelected(filePath);
                    folderModel.folder = filePath;
                }
                else {
                    folderList.fileSelected(filePath);
                }
            }
        }
    }

    ListView {
        id: folderListView
        anchors.fill: parent
        model: folderModel
        header: folderListHeader
        delegate: folderListItem
    }
}

// End of file.
